package com.sgb.member.view_model;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.BiddingRegisterBean;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.ui.fragment.CompanyWebBiddingFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Time:2021/4/30
 * AUTHOR:F_Q
 * Description:
 */
public class CompanyWebBiddingModel extends BaseViewModel {

    CompanyWebBiddingFragment fragment;
    MemberRepertory repertory;
    int current;

    public CompanyWebBiddingModel(CompanyWebBiddingFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }

    public void getBiddingData(boolean isLoadMore, String companyNo) {
        showOrHideDialog(true);
        int curPager = isLoadMore ? current + 1 : 1;
        int maxPageSize = 10;
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("page", curPager);
        map.put("limit", maxPageSize);
        map.put("isMemberZc", "1");
        map.put("companyNo", companyNo);
        repertory.getApi().getCompanyWebBidderList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<BiddingRegisterBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<BiddingRegisterBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<BiddingRegisterBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFinally() {
                showOrHideDialog(false);
            }
        });
    }


}
